package com.shanghaionstar.user.presenter;

import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.smart.BaseResponse;
import com.shanghaionstar.user.bean.ReplenishEntity;
import com.shanghaionstar.user.iview.ReplenishView;
import com.shanghaionstar.user.service.ReplenishModel;

import rx.functions.Action1;
/**
 * 补充info3用户信息
 *
 * Created by Yonglang Hong on 2016/11/15.
 */
public class ReplenishPresenter extends BasePresenter{

    ReplenishModel model;

    ReplenishView  view;

    public ReplenishPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
        this.model = new ReplenishModel();
    }

    public void setView(ReplenishView view){
        this.view = view;
    }

    public void replenish(ReplenishEntity entity){
        subscription = model.replenish(entity)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<BaseResponse>() {
                    @Override
                    public void call(BaseResponse response) {
                        if (view != null) {
                            if ("E0000".equals(response.code))
                                view.onSuccess();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (view != null && throwable != null) {
                            view.onFailed(throwable);
                        }
                    }
                });
    }

    public void validateNeedName(String vin){
        subscription = model.validateNeedName(vin)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<BaseResponse>() {
                    @Override
                    public void call(BaseResponse response) {
                        if (view != null) {
                            if ("E0000".equals(response.code))
                                if ("true".equalsIgnoreCase(response.description))
                                    view.needName(true);
                            else
                                    view.needName(false);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (view != null && throwable != null) {
                            view.onFailed(throwable);
                        }
                    }
                });
    }
}
